﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using NewDDSys.Entity;
using NewDDSys.IBLL;
using NewDDSys.IDAL;
using NewDDSys.Factory;
using NewDDSys.Utility;


using NewDDSys.DATAOUT;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Web_SCTJ : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        

        ExcelEdit report = new ExcelEdit();
        report.CreateExcel();
        report.CreateWorkSheet("生产数据统计表");
        report.CellsUnite(1, 1, 1, 14);
        report.WriteData("生产数据统计", 1, 1);
        report.CellsAlignment(1, 1, 1, 14, ExcelEdit.ExcelHAlign.居中, ExcelEdit.ExcelVAlign.居中);
        report.WriteData("查询日期", 2, 1);
        report.WriteData("库房", 2, 2);
        report.WriteData("总站计划", 2, 3);
        report.WriteData("外发车数", 2, 4);
        report.WriteData("外发吨位", 2, 5);
        report.WriteData("外发时长", 2, 6);
        report.WriteData("外发延时", 2, 7);
        report.WriteData("装载返厂车数", 2, 8);
        report.WriteData("装载返厂吨位", 2, 9);
        report.WriteData("零提车数", 2, 10);
        report.WriteData("零提吨位", 2, 11);
        report.WriteData("倒库吨位", 2, 12);
        report.WriteData("库存", 2, 13);
        report.WriteData("零提计划量", 2, 14);
        report.WriteData("外发计划综合兑现率", 2, 15);
        string cday = txtSday.Text.ToString();
        report.WriteData(cday, 3, 1);
        report.WriteData("嘉东库", 3, 2);
        string jh = BLLFactory.CreateHcrBLL().JDJH(cday);
        report.WriteData(jh, 3, 3);
        string t1 = Convert.ToDateTime(cday).AddHours(-4).ToString();
        string t2 = Convert.ToDateTime(cday).AddHours(8).ToString();
        string t3 = Convert.ToDateTime(cday).AddHours(20).ToString();
        DataSet ds1 = BLLFactory.CreateWorkBLL().JDWF(cday);
        string dkwfc = ds1.Tables[0].Rows[0]["wfc"].ToString();
        string dkwfd = ds1.Tables[0].Rows[0]["wfd"].ToString();
        string dkwfs = ds1.Tables[0].Rows[0]["wfs"].ToString();
        string dkwfy = ds1.Tables[0].Rows[0]["wfy"].ToString();
        DataSet ds2 = BLLFactory.CreateDxcBLL().JDYCL(t1, "外发");
        string dxccy = ds2.Tables[0].Rows[0]["csy"].ToString();
        string dxcdy = ds2.Tables[0].Rows[0]["duny"].ToString();
        DataSet ds3 = BLLFactory.CreateDxcBLL().JDWCL(t1, "外发");
        string dxccw = ds3.Tables[0].Rows[0]["csw"].ToString();
        string dxcdw = ds3.Tables[0].Rows[0]["dunw"].ToString();
        decimal sjcs = 0;
        int cs = 0;
        decimal wds = 0;
        decimal ws = 0;
        decimal wy = 0;
        int wfc = 0;
        if (dkwfc != "")
        {
            wfc = Convert.ToInt32(dkwfc);
            cs = wfc;
            wds = Convert.ToDecimal(dkwfd);
            ws = Convert.ToDecimal(dkwfs);
            wy = Convert.ToDecimal(dkwfy);
        }
        wfc = 0;    
        int ycs = 0;
        if (dxccy != "")
        {
            ycs = Convert.ToInt32(dxccy);
            cs = ycs + cs;
            wds = wds + Convert.ToDecimal(dxcdy);
        }
        ycs = 0;    
        int wcs = 0;
        if (dxccw != "")
        {
            wcs = Convert.ToInt32(dxccw);
            cs = cs - wcs;
            wds = wds - Convert.ToDecimal(dxcdw);
        }
        wcs = 0;
        report.WriteData(cs.ToString(), 3, 4);
        report.WriteData(wds.ToString("f2"), 3, 5);
        report.WriteData(ws.ToString("f2"), 3, 6);
        report.WriteData(wy.ToString("f2"), 3, 7);
        sjcs = cs;
        cs = 0;
        wds = 0;
        ws = 0;
        wy = 0;
        DataSet ds4 = BLLFactory.CreateFcBLL().fccd(cday, "嘉东库");
        report.WriteData(ds4.Tables[0].Rows[0]["cs"].ToString(), 3, 8);
        report.WriteData(ds4.Tables[0].Rows[0]["fcds"].ToString(), 3, 9);
        DataSet ds5 = BLLFactory.CreateWorkBLL().JDLT(cday);
        string ltc = ds5.Tables[0].Rows[0]["wfc"].ToString();
        string ltd = ds5.Tables[0].Rows[0]["wfd"].ToString();
        ds2 = BLLFactory.CreateDxcBLL().JDYCL(t1, "零提");
        dxccy = ds2.Tables[0].Rows[0]["csy"].ToString();
        dxcdy = ds2.Tables[0].Rows[0]["duny"].ToString();
        ds3 = BLLFactory.CreateDxcBLL().JDWCL(t1, "零提");
        dxccw = ds3.Tables[0].Rows[0]["csw"].ToString();
        dxcdw = ds3.Tables[0].Rows[0]["dunw"].ToString();
        if (ltc != "")
        {
            wfc = Convert.ToInt32(ltc);
            cs = wfc;
            wds = Convert.ToDecimal(ltd);
        }
        wfc = 0;
        if (dxccy != "")
        {
            ycs = Convert.ToInt32(dxccy);
            cs = ycs + cs;
            wds = wds + Convert.ToDecimal(dxcdy);
        }
        ycs = 0;
        if (dxccw != "")
        {
            wcs = Convert.ToInt32(dxccw);
            cs = cs - wcs;
            wds = wds - Convert.ToDecimal(dxcdw);
        }
        wcs = 0;

        report.WriteData(cs.ToString(), 3, 10);
        report.WriteData(wds.ToString("f2"), 3, 11);
        sjcs = sjcs + wds / 60;
        cs = 0;
        wds = 0;
        string dkd = BLLFactory.CreateWorkBLL().JDDK(cday);
        ds2 = BLLFactory.CreateDxcBLL().JDYCL(t1, "倒库");
        dxcdy = ds2.Tables[0].Rows[0]["duny"].ToString();
        ds3 = BLLFactory.CreateDxcBLL().JDWCL(t1, "倒库");
        dxcdw = ds3.Tables[0].Rows[0]["dunw"].ToString();
        if (dkd != "")
        {

            wds = Convert.ToDecimal(dkd);
        }

        if (dxcdy != "")
        {
            
            wds = wds + Convert.ToDecimal(dxcdy);
        }
        if (dxcdw != "")
        {
            
            wds = wds - Convert.ToDecimal(dxcdw);
        }
        report.WriteData(wds.ToString("f2"), 3, 12);
        wds = 0;
        string dkxc = BLLFactory.CreateXkcdtBLL().JDXC(cday);
        string dkbc = BLLFactory.CreateBkcdtBLL().JDBC(cday);
        string dkjb = BLLFactory.CreateRkcdtBLL().JDJB(cday);
        string dkzb = BLLFactory.CreateZkcdtBLL().JDZB(cday);
        decimal kc = 0;
        if (dkxc != "")
            kc = Convert.ToDecimal(dkxc);
        if (dkbc != "")
            kc = kc + Convert.ToDecimal(dkbc);
        if(dkjb != "")
            kc = kc + Convert.ToDecimal(dkjb);
        if(dkzb != "")
            kc = kc + Convert.ToDecimal(dkzb);
        report.WriteData(kc.ToString("f2"), 3, 13);
        kc = 0;
        string ltjh = BLLFactory.CreateLtrBLL().JDJH(cday);
        decimal jhlt = 0;
        decimal dxl = 0;
        if (ltjh != "")
        {
            jhlt = Convert.ToDecimal(ltjh);
            report.WriteData(jhlt.ToString("f2"), 3, 14);
        }
        else
            report.WriteData("无总站零提计划！", 3, 14);
        int zz = 0;
        if (jh != "")
        {
            zz = Convert.ToInt32(jh);
            dxl = (sjcs / (zz + (jhlt / 60))) * 100;
            report.WriteData(dxl.ToString("f2"), 3, 15);
            dxl = 0;
            zz = 0;
            jhlt = 0;
        }
        else
            report.WriteData("无总站火车计划！", 3, 15);
            
        string[] s1 = { "板材库", "不锈钢热轧库", "不锈钢冷轧库", "碳钢热轧库", "碳钢冷轧库", "一高线", "二高线", "大棒线", "四轧库", "酸洗库", "化产库" };
        int k = 4;
        foreach (string ku in s1)
        {


            report.WriteData(cday, k, 1);
            report.WriteData(ku, k, 2);
            jh = BLLFactory.CreateHcrBLL().KURJH(ku, cday);
            
            report.WriteData(jh, k, 3);
            ds1 = BLLFactory.CreateWorkBLL().nld("外发", ku, cday);
            dkwfc = ds1.Tables[0].Rows[0]["cs"].ToString();
            dkwfd = ds1.Tables[0].Rows[0]["l"].ToString();
            ds2 = BLLFactory.CreateWorkBLL().timetj(ku, cday);
            dkwfs = ds2.Tables[0].Rows[0]["sc"].ToString();
            dkwfy = ds2.Tables[0].Rows[0]["ystj"].ToString();
            ds2 = BLLFactory.CreateDxcBLL().NRYCL(t2, ku, "外发");
            dxccy = ds2.Tables[0].Rows[0]["csy"].ToString();
            dxcdy = ds2.Tables[0].Rows[0]["duny"].ToString();
            ds3 = BLLFactory.CreateDxcBLL().NRWCL(t3, ku, "外发");
            dxccw = ds3.Tables[0].Rows[0]["csw"].ToString();
            dxcdw = ds3.Tables[0].Rows[0]["dunw"].ToString();
            if (dkwfc != "")
            {
                wfc = Convert.ToInt32(dkwfc);
                cs = wfc;
                wds = Convert.ToDecimal(dkwfd);
                ws = Convert.ToDecimal(dkwfs);
                wy = Convert.ToDecimal(dkwfy);
            }
            wfc = 0;
            
            if (dxccy != "")
            {
                ycs = Convert.ToInt32(dxccy);
                cs = ycs + cs;
                wds = wds + Convert.ToDecimal(dxcdy);
            }
            ycs = 0;
            
            if (dxccw != "")
            {
                wcs = Convert.ToInt32(dxccw);
                cs = cs - wcs;
                wds = wds - Convert.ToDecimal(dxcdw);
            }
            wcs = 0;
            report.WriteData(cs.ToString(), k, 4);
            report.WriteData(wds.ToString("f2"), k, 5);
            report.WriteData(ws.ToString("f2"), k, 6);
            report.WriteData(wy.ToString("f2"), k, 7);
            sjcs = cs;
            cs = 0;
            wds = 0;
            ws = 0;
            wy = 0;
            ds4 = BLLFactory.CreateFcBLL().fccd(cday, ku);
            report.WriteData(ds4.Tables[0].Rows[0]["cs"].ToString(), k, 8);
            report.WriteData(ds4.Tables[0].Rows[0]["fcds"].ToString(), k, 9);
            ds5 = BLLFactory.CreateWorkBLL().nld("零提", ku, cday);
            ltc = ds4.Tables[0].Rows[0]["cs"].ToString();
            ltd = ds4.Tables[0].Rows[0]["l"].ToString();
            ds2 = BLLFactory.CreateDxcBLL().NRYCL(t2, ku, "零提");
            dxccy = ds2.Tables[0].Rows[0]["csy"].ToString();
            dxcdy = ds2.Tables[0].Rows[0]["duny"].ToString();
            ds3 = BLLFactory.CreateDxcBLL().NRWCL(t3, ku, "零提");
            dxccw = ds3.Tables[0].Rows[0]["csw"].ToString();
            dxcdw = ds3.Tables[0].Rows[0]["dunw"].ToString();
            if (ltc != "")
            {
                wfc = Convert.ToInt32(ltc);
                cs = wfc;
                wds = Convert.ToDecimal(ltd);
            }
            wfc = 0;

            if (dxccy != "")
            {
                ycs = Convert.ToInt32(dxccy);
                cs = ycs + cs;
                wds = wds + Convert.ToDecimal(dxcdy);
            }
            ycs = 0;

            if (dxccw != "")
            {
                wcs = Convert.ToInt32(dxccw);
                cs = cs - wcs;
                wds = wds - Convert.ToDecimal(dxcdw);
            }
            wcs = 0;
            report.WriteData(cs.ToString(), k, 10);
            report.WriteData(wds.ToString("f2"), k, 11);
            sjcs = sjcs + wds / 60;
            cs = 0;
            wds = 0;
            ds5 = BLLFactory.CreateWorkBLL().nld("倒库", ku, cday);
            dkd = ds5.Tables[0].Rows[0]["l"].ToString();
            ds2 = BLLFactory.CreateDxcBLL().NRYCL(t2, ku, "倒库");
            dxcdy = ds2.Tables[0].Rows[0]["duny"].ToString();
            ds3 = BLLFactory.CreateDxcBLL().NRWCL(t3, ku, "倒库");
            dxcdw = ds3.Tables[0].Rows[0]["dunw"].ToString();
            if (dkd != "")
            {

                wds = Convert.ToDecimal(dkd);
            }

            if (dxcdy != "")
            {

                wds = wds + Convert.ToDecimal(dxcdy);
            }
            if (dxcdw != "")
            {

                wds = wds - Convert.ToDecimal(dxcdw);
            }
            report.WriteData(wds.ToString("f2"), k, 12);
            wds = 0;
            string Nkukc = "";
            decimal Nkc = 0;
            switch (ku)
            {
                case "一高线":
                    {
                        string xckc = BLLFactory.CreateXkcdtBLL().KuKC(cday, ku);
                        decimal Nxckc = 0;
                        if (xckc != "")
                            Nxckc = Convert.ToDecimal(xckc);
                        decimal Nbckc = 0;
                        string bckc = BLLFactory.CreateBkcdtBLL().KuKC(cday, ku);
                        if (bckc != "")
                            Nbckc = Convert.ToDecimal(bckc);

                        report.WriteData((Nxckc + Nbckc).ToString("f2"), k, 13);
                        
                    }; break;
                case "二高线":
                    {
                        
                        Nkukc = BLLFactory.CreateXkcdtBLL().KuKC(cday, ku);
                        if (Nkukc != "")
                            Nkc = Convert.ToDecimal(Nkukc);
                        report.WriteData((Nkc).ToString("f2"), k, 13);
                        
                        Nkc = 0;
                    }; break;
                case "大棒线":
                case "四轧库":
                    {
                        
                        Nkukc = BLLFactory.CreateBkcdtBLL().KuKC(cday, ku);
                        if (Nkukc != "")
                            Nkc = Convert.ToDecimal(Nkukc);
                        report.WriteData((Nkc).ToString("f2"), k, 13);
                        
                        Nkc = 0;
                    }; break;

                case "不锈钢热轧库":
                case "碳钢热轧库":
                case "酸洗库":
                    {
                        
                        Nkukc = BLLFactory.CreateRkcdtBLL().KuKC(cday, ku);
                        if (Nkukc != "")
                            Nkc = Convert.ToDecimal(Nkukc);
                        report.WriteData((Nkc).ToString("f2"), k, 13);
                        Nkc = 0;
                    }; break;
                case "板材库":
                    {
                        
                        Nkukc = BLLFactory.CreateZkcdtBLL().KuKC(cday);
                        if (Nkukc != "")
                            Nkc = Convert.ToDecimal(Nkukc);
                        report.WriteData((Nkc).ToString("f2"), k, 13);
                        Nkc = 0;
                    }; break;
                case "不锈钢冷轧库":
                case "碳钢冷轧库":
                    {
                       
                        Nkukc = BLLFactory.CreateLkcdtBLL().KuKC(cday, ku);
                        if (Nkukc != "")
                            Nkc = Convert.ToDecimal(Nkukc);
                        report.WriteData((Nkc).ToString("f2"), k, 13);
                        Nkc = 0;
                    }; break;
                case "化产库":
                    {
                        
                        Nkukc = BLLFactory.CreateHkcdtBLL().KuKC(cday);
                        if (Nkukc != "")
                            Nkc = Convert.ToDecimal(Nkukc);
                        report.WriteData((Nkc).ToString("f2"), k, 13);
                        Nkc = 0;
                    }; break;
                default:
                    break;
            }
            ltjh = BLLFactory.CreateLtrBLL().KURJH(ku, cday);
            
            if (ltjh != "")
            {
                jhlt = Convert.ToDecimal(ltjh);
                report.WriteData(jhlt.ToString("f2"), k, 14);
            }
            else
                report.WriteData("无总站零提计划！", k, 14);
            if (jh != "")
            {
                zz = Convert.ToInt32(jh);
                dxl = (sjcs / (zz + (jhlt / 60))) * 100;
                report.WriteData(dxl.ToString("f2"), k, 15);
                dxl = 0;
                zz = 0;
                jhlt = 0;
            }
            else
                report.WriteData("无总站火车计划！", k, 15);
            

        }
        
        //"sctj\\"
        string title = Session["用户信息.单位"].ToString() + Session["用户信息.姓名"].ToString() + cday + "生产数据统计";
        string savepath = "sctj\\" + title + ".xls";
        string RepPath = Server.MapPath(savepath);

        report.SaveAs(RepPath); //文档路径
        report.Close();
        string filename = title + ".xls";

        string spath = "sctj\\" + filename;
        string RPath = Server.MapPath(spath);
        FileInfo f = new FileInfo(RPath);
        if (f.Exists)
        {

            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            Response.ContentType = "application/msexcel";
            Response.WriteFile(RPath);
            Response.Flush();
            Response.Close();
            f.Delete();
        }

        
    }
    
    
    
}
